package com.csi.okr.mobile.company.controller;

import com.csi.okr.common.util.BasicValueUtil;
import com.csi.okr.common.util.WxUtil;
import com.csi.okr.mobile.common.task.DataTask;
import com.csi.okr.mobile.company.service.CompanyService;
import com.csi.okr.mobile.self.service.SelfService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/com")
public class CompanyController {

	@Resource
	private CompanyService compService;
	@Resource
	private SelfService selfService;
	
	
	/**
	 * 微信请求入口 - 当前
	 * @return
	 */
    @RequestMapping(value = "/oauth")
    public ModelAndView oauth() {
    	//回调地址
    	
//    	http://okr.yanxishe.cc/okr/org/callback?code=42ba46d2be11158754ffa861d1573fa0&state=oauth
    	String redirect_url = WxUtil.DOMAIN + "/com/callback";
    	//授权地址
    	StringBuilder url = new StringBuilder();
    	url.append("https://open.weixin.qq.com/connect/oauth2/authorize");
    	url.append("?appid="+WxUtil.CORPID);
    	url.append("&redirect_uri="+redirect_url);
    	url.append("&response_type=code");
    	url.append("&scope=snsapi_base");
    	url.append("&state=oauth");
    	url.append("#wechat_redirect");
        ModelAndView mv = new ModelAndView();
        mv.setView(new RedirectView(url.toString()));
        return mv;
    }
    
    /**
     * 微信请求入口 - 查询
     * @return
     */
    @RequestMapping(value = "/query")
    public ModelAndView query() {
    	//回调地址
    	String redirect_url = WxUtil.DOMAIN + "/com/callback";
    	//授权地址
    	StringBuilder url = new StringBuilder();
    	url.append("https://open.weixin.qq.com/connect/oauth2/authorize");
    	url.append("?appid="+WxUtil.CORPID);
    	url.append("&redirect_uri="+redirect_url);
    	url.append("&response_type=code");
    	url.append("&scope=snsapi_base");
    	url.append("&state=query");
    	url.append("#wechat_redirect");
    	ModelAndView mv = new ModelAndView();
    	mv.setView(new RedirectView(url.toString()));
    	return mv;
    }
    
    @RequestMapping(value = "/callback")
    public ModelAndView callback(@RequestParam String code,String state,HttpSession session) {
    	
    	String memberCode = WxUtil.getEmplCode(code);
    	
    	if(memberCode == null)
    		return oauth();
    	
    	if (memberCode.equals("index") || memberCode.equals("lixun") || memberCode.equals("E100021495")|| memberCode.equals("lixun2")) {
    		memberCode = "E000100834";
//    		E100014295
//    		E000100834
		} 
    	
    	ModelAndView mv = new ModelAndView();
		mv.setViewName("com/comokr");
		//当前登录人员员工编号
		mv.addObject("memberCode", memberCode);
		mv.addObject("param", state);
//		String orgCode = "0";
		String orgCode = compService.getDefaultOrgCode(memberCode).toString();
		mv.addObject("orgCode", orgCode);
		mv.addObject("orgName", compService.getOrgByOrgCode(orgCode));
		
		
		session.setAttribute("tree", compService.getTopOrg());
		session.setAttribute("years", BasicValueUtil.getDate2Now());
		session.setAttribute("oColor", DataTask.OCOLOR);
		session.setAttribute("krColor", DataTask.KRCOLOR);
		session.setAttribute("oState", DataTask.O_STATE);
		session.setAttribute("oTeam", DataTask.O_TEAM);
		session.setAttribute("rState", DataTask.R_STATE);
		session.setAttribute("sState", DataTask.S_STATE);
		return mv;
    }
    
    @RequestMapping(value = "/index")
    public ModelAndView index(@RequestParam String code,String param,HttpSession session) {
    	
    	ModelAndView mv = new ModelAndView();
    	mv.setViewName("com/comokr");
    	mv.addObject("memberCode", code);
    	mv.addObject("param", param);
    	String orgCode = compService.getDefaultOrgCode(code).toString();
		mv.addObject("orgCode", orgCode);
		mv.addObject("orgName", compService.getOrgByOrgCode(orgCode));
    	
    	session.setAttribute("tree", compService.getTopOrg());
    	session.setAttribute("oColor", DataTask.OCOLOR);
    	session.setAttribute("krColor", DataTask.KRCOLOR);
    	session.setAttribute("oState", DataTask.O_STATE);
    	session.setAttribute("oTeam", DataTask.O_TEAM);
    	session.setAttribute("rState", DataTask.R_STATE);
    	session.setAttribute("sState", DataTask.S_STATE);
    	return mv;
    }

	/**
	 * 组织树点击跳转方法
	 * @param memberCode
	 * @param orgCode
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/tree", method = RequestMethod.GET)
	public ModelAndView tree(@RequestParam String memberCode,String orgCode,HttpSession session) {
		
		ModelAndView mv = new ModelAndView();
		mv.setViewName("com/comokr");
		mv.addObject("orgCode", orgCode);
		mv.addObject("memberCode", memberCode);
		mv.addObject("orgName", compService.getOrgByOrgCode(orgCode));
		return mv;
	}

	/**
	 * 右侧部门成员
	 * 
	 * @param orgCode
	 * @return
	 */
	@RequestMapping(value = "/subset", method = RequestMethod.POST)
	@ResponseBody
	public Object subset(@RequestParam String orgCode,String memberCode,String year,String month,String quarter,HttpSession session) {

		return compService.getSubsetOrg(orgCode, memberCode, year, month, quarter);
	}
	
	/**
	 * 查询部门下子部门
	 * @param orgCode
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/child", method = RequestMethod.POST)
	@ResponseBody
	public Object child(@RequestParam String orgCode,HttpSession session) {
	
		return compService.getChildOrg(orgCode);
	}

	/**
	 * 跳转成员OKR
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/member", method = RequestMethod.GET)
	public ModelAndView member(@RequestParam String emplCode,String memberCode,String orgCode) {

		ModelAndView mv = new ModelAndView();
		mv.addObject("orgCode",orgCode);
		mv.addObject("emplCode",emplCode);
		mv.addObject("memberCode",memberCode);
		mv.addObject("org", compService.getTopOrg());
		
		mv.setViewName("com/emplokr");
		
		return mv;
	}
	
	/**
	 * 查询成员OKR可见数据
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/okr", method = RequestMethod.POST)
	@ResponseBody
	public Object okr(@RequestParam int pageIndex,int pageSize,String emplCode,String memberCode,String orgCode, String month, String year, String quarter,
			String state, String odefinition) {
		
		if(emplCode.equals(memberCode)){
			return selfService.getOkrByEmplCode(pageIndex, pageSize,orgCode, emplCode,
					month, year, quarter, state, odefinition);
		}

		return compService.getOkrByMemberCode(pageIndex,pageSize,emplCode,orgCode,memberCode,
				month, year, quarter, state, odefinition);
	}
	
	/**
	 * 跳转review页面
	 * @param
	 * @param
	 * @param uniqueCode
	 * @return
	 */
	@RequestMapping(value = "/review", method = RequestMethod.GET)
	public ModelAndView review(@RequestParam String uniqueCode) {

		ModelAndView mv = new ModelAndView();
		mv.addObject("uniqueCode",uniqueCode);
		mv.setViewName("review");
		
		return mv;
	}
	
	/**
	 * 跳转review详情页面
	 * @param
	 * @param
	 * @param uniqueCode
	 * @param reviewUuid
	 * @return
	 */
	@RequestMapping(value = "/reviewd", method = RequestMethod.GET)
	public ModelAndView reviewd(@RequestParam String uniqueCode,String reviewUuid) {
		
		ModelAndView mv = new ModelAndView();
		mv.addObject("uniqueCode",uniqueCode);
		mv.addObject("reviewUuid",reviewUuid);
		mv.setViewName("reviewd");
		
		return mv;
	}
	
	/**
	 * 跳转历史版本
	 * @param
	 * @param
	 * @param uniqueCode
	 * @return
	 */
	@RequestMapping(value = "/version", method = RequestMethod.GET)
	public ModelAndView version(@RequestParam String uniqueCode) {
		
		ModelAndView mv = new ModelAndView();
		mv.addObject("uniqueCode",uniqueCode);
		mv.setViewName("version");
		
		return mv;
	}
	
	/**
	 * 跳转版本信息
	 * @param
	 * @param
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/versiond", method = RequestMethod.GET)
	public ModelAndView versiond(@RequestParam String versionUuid) {
		
		ModelAndView mv = new ModelAndView();
		mv.addObject("versionUuid",versionUuid);
		mv.setViewName("versiond");
		
		return mv;
	}


}
